<?php
ini_set('memory_limit', '512M'); // increase this if you catch "Allowed memory size..."
require_once './config.php';
require_once './dbclass2.php';
require_once './global.php';
require_once 'vendor/ChipVN/ClassLoader/Loader.php';
require_once 'vendor/PhpThumb/ThumbLib.inc.php';
ChipVN_ClassLoader_Loader::registerAutoload();
function geturl2($url){
	
$cookie ="cookie.txt";
$userAgent = 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31' ;
$ch = curl_init($url);
$options = array(
    CURLOPT_CONNECTTIMEOUT => 20 , 
    CURLOPT_USERAGENT => $userAgent,
    CURLOPT_AUTOREFERER => true,
    
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_COOKIEFILE => $cookie,
    CURLOPT_COOKIEJAR => $cookie ,
    CURLOPT_SSL_VERIFYPEER => 0 ,
    CURLOPT_SSL_VERIFYHOST => 0
);
curl_setopt_array($ch, $options);
$str = curl_exec($ch);
curl_close($ch);
return $str;
}

$config = load_file('includes/config.php');
$options = $config['options'];

$defaults = array();
foreach ($options as $name => $option) {
    $defaults[$name] = $option['default'];
}
extract(array_map('trim', $_POST) + $defaults + array('type' => '', 'url' => ''), EXTR_PREFIX_ALL, 'data');

if (file_exists($demo = file_path('demo.php'))) include $demo;

if (in_array($data_type, array('upload', 'transload'))) {
    // Validate
    foreach (array_keys($defaults) as $name) {
        $varname = 'data_' . $name;
        if ($options[$name]['options'] && !in_array(${$varname}, array_keys($options[$name]['options']))) {
            response_json(array(
                'error'   => true,
                'message' => 'The value of "' . $name . '" is invalid.'
            ));
        }
    }
    $tempFile = $config['temp_dir'] . '/' . uniqid() . '.jpg';
    // remove comment under if you want to keep original file name
    // some service will force file name to their name (eg: imgur)
    if (isset($_FILES['files']['name'][0])) {
        $tempFile = $config['temp_dir'] . '/' . $_FILES['files']['name'][0];
    }
    try {
        if ($data_type == 'upload' && !empty($_FILES['files'])) {
            $file = array(
                'name'     => $_FILES['files']['name'][0],
                'size'     => $_FILES['files']['size'][0],
                'type'     => $_FILES['files']['type'][0],
                'tmp_name' => $_FILES['files']['tmp_name'][0],
            );
            if (!$imageSize = getimagesize($file['tmp_name'])) {
                throw new Exception('The file is not an image.');
            }
            if ($file['size'] > $config['upload']['max_file_size']) {
                throw new Exception('The image is too large.');
            }
            $phpThumb = PhpThumbFactory::create($file['tmp_name']);

        } elseif ($data_type == 'transload' && parse_url($data_url, PHP_URL_HOST)) {
            if (download_file($data_url, $tempFile)) {
                if (!$imageSize = getimagesize($tempFile)) {
                    throw new Exception('The url is not an image.');
                }
            } else {
                throw new Exception('Cannot download the url.');
            }
            $phpThumb = PhpThumbFactory::create($tempFile);

        } else {
            throw new Exception('Data is invalid.');
        }
        $phpThumb->setOptions(array(
            'resizeUp'              => false,
            'correctPermissions'    => false,
            // 'preserveAlpha'         => false,
            // 'preserveTransparency'  => false,
        ));
        $logo    = $config['logo_dir'] . '/' . $data_watermark_logo . '.png';
        $minSize = explode('x', $config['watermark_minimum_size']);
        if (
            $data_watermark
            && file_exists($logo)
            && ( empty($config['watermark_minimum_size'])
                || (count($minSize) == 2
                    && $minSize[0] <= $imageSize[0]
                    && $minSize[1] <= $imageSize[1]
                )
            )
        ) {
            $phpThumb
                ->resize($data_resize)
                ->createWatermark($logo, $data_watermark_position, 0);
        } else {
            $phpThumb->resize($data_resize);
        }
        $phpThumb->save($tempFile);

    } catch (Exception $e) {
        if (file_exists($tempFile)) {
            unlink($tempFile);
        }
        response_json(array(
            'error'   => true,
            'message' => $e->getMessage()
        ));
    }

	//GET LINK IMAGE--------
	$link=str_replace("/home/truyen/domains/truyencuatui.info/public_html/","http://truyencuatui.info/",$tempFile);
	$link = str_replace(' ', '%20', $link);

	//-----End

    $server = strtolower($data_server);
    // setup general
  
    $result = array();
    try {
      
        switch ($server) {
            case 'imgur':
             	$url="http://up.manga24h.com/upload_remote.php?link=$link&server=imgur";
                break;

         case 'picasa':
				$url="http://up.manga24h.com/upload_remote.php?link=$link&server=picasa";
                break;
				
             

          
        }
        // group cache identifier is made by plugin name, username
        // so we should call this after call login();
	
		
    	echo geturl2($url);
		unlink($tempFile);
		exit();
    } catch (Exception $e) {
        $result = array(
            'error'   => true,
            'message' => $e->getMessage()
        );
    }
    unlink($tempFile);

    response_json($result);
}
